#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "comm.h"
#include "menu.h"
#include "device.h"

void empty_buffer()
{
	int ch;
	while((ch = getchar()) != '\n' && ch != EOF)
		;
}

int welcome_menu(node_t *devlist)
{
	while(1)
	{
		system("clear"); // Linux的清屏命令
    	printf("|--------- Smart Home Device Management System ---------|\n");
		printf("|  > Welcome                                            |\n");
		printf("|-------------------------------------------------------|\n");
		printf("|  1. Login(登录)                                       |\n");
		printf("|  q. Quit                                              |\n");
		printf("|                                                       |\n");
		printf("|                                                       |\n");
		printf("|-------------------------------------------------------|\n");
		printf("| Select your option(选择你的选项)                      |\n");
		printf("|-------------------------------------------------------|\n");

		int op = getchar(); // getchar()能且只能从输入中获取一个字符
		empty_buffer();
		if('1' == op)
		{
			login_menu(devlist);
		}
		else if('q' == op)
		{
			return op;
		}
		else
		{
			printf("Your input is out of range.\n");
			return -1;
		}
	}

	return 0;
}

int login_menu(node_t *devlist)
{
	int error_num = 0;
	char input_name[16] = {0};
	char input_pass[16] = {0};

	while(error_num < LOOPCOUNT)
	{
		system("clear");
		printf("|--------- Smart Home Device Management System ---------|\n");
		printf("|  > Welcome > Login                                    |\n");
		printf("|-------------------------------------------------------|\n");
		printf("|  q. Quit                                              |\n");
		printf("|                                                       |\n");
		printf("|                                                       |\n");
		printf("|                                                       |\n");
		printf("|-------------------------------------------------------|\n");
		printf("| Input your username and password                      |\n");
		printf("|-------------------------------------------------------|\n");

		printf("Input username: ");
		scanf("%15s", input_name);
		empty_buffer();
		if(strcmp(input_name, "q") == 0)
		{
			return 'q';
		}
		
		printf("Input password: ");
		scanf("%15s", input_pass);
		empty_buffer();
		if(strcmp(input_pass, "q") == 0)
		{
			return 'q';
		}

		if((strcmp(input_name, ADMIN_NAME) == 0) && (strcmp(input_pass, ADMIN_PASS) == 0))
		{
			admin_menu(devlist);
		}
		else
		{
			printf("Wrong username or password\n");
			getchar();
			error_num++;
			continue;
		}
	}

	if(error_num >= LOOPCOUNT)
	{
		printf("Number of errors exceeds the limit.\n");
		empty_buffer();
		return -1;
	}

	return 0;
}

int admin_menu(node_t *devlist)
{
	while(1)
	{
		system("clear");
		printf("|--------- Smart Home Device Management System ---------|\n");
		printf("|  > Welcome > Login > Admin                            |\n");
		printf("|-------------------------------------------------------|\n");
		printf("|  1. Add Device             2. Delete Device           |\n");
		printf("|  3. Update Device(更新)    4. Display Device(显示)    |\n");
		printf("|  5. Find Device            q. Quit                    |\n");
		printf("|                                                       |\n");
		printf("|-------------------------------------------------------|\n");
		printf("| Select your operation(选择你的操作)                   |\n");
		printf("|-------------------------------------------------------|\n");
		printf("\n");
		printf("---------------------- Device List ----------------------\n");
		printf("Id\tName\tType\tIp\tPower\n");
		show_dev(devlist, showfun_dev_full);
		printf("---------------------------------------------------------\n");

		int op = getchar();
		empty_buffer();

		if('1' == op)
		{
			add_dev_menu(devlist);
		}
		else if('2' == op)
		{
			del_dev_menu(devlist);
		}
		else if('3' == op)
		{
			update_dev_menu(devlist);
		}
		else if('4' == op)
		{
			show_dev_menu(devlist);
		}
		else if('5' == op)
		{
			find_dev_menu(devlist);
		}
		else if('q' == op)
		{
			return -1;
		}
		else
		{
			continue;
		}
	}

	return 0;
}

int add_dev_menu(node_t *devlist)
{
	device_t new_dev;
	memset(&new_dev, 0, sizeof(device_t));

	while(1)
	{
		system("clear");
		printf("|--------- Smart Home Device Management System ---------|\n");
		printf("|  > Welcome > Login > Admin > Add Device               |\n");
		printf("|-------------------------------------------------------|\n");
		printf("|  y. Yes                                               |\n");
		printf("|  n. No                                                |\n");
		printf("|  q. Quit                                              |\n");
		printf("|                                                       |\n");
		printf("|-------------------------------------------------------|\n");
		printf("| Input new device information                          |\n");
		printf("|-------------------------------------------------------|\n");
		printf("\n");
		printf("---------------------- Device List ----------------------\n");
		printf("Id\tName\tType\tIp\tPower\n");
		show_dev(devlist, showfun_dev_full);
		printf("---------------------------------------------------------\n");

		printf("Input device name\t: ");
		scanf("%s", new_dev.dev_name);
		empty_buffer();

		printf("Input device type\t: ");
		scanf("%s", new_dev.dev_type);
		empty_buffer();

		printf("Input device ip\t\t: ");
		scanf("%s", new_dev.dev_ip);
		empty_buffer();

		printf("Input device power\t: ");
		scanf("%f", &new_dev.dev_power);
		empty_buffer();

		printf("Correct?(y/n/q): ");
		int op = getchar();
		empty_buffer();
		if('y' == op)
		{
			// new_dev.dev_id = generate_id(); // 错误调用
			new_dev.dev_id = g_dev_id_min;
			add_dev(devlist, &new_dev);
			break;
		}
		else if('n' == op || 'q' == op)
		{
			break;
		}
		else
		{
			continue;
		}
	}

	return 0;
}

int del_dev_menu(node_t *devlist)
{
	device_t delete_dev;
	memset(&delete_dev, 0, sizeof(delete_dev));

	while(1)
	{
		system("clear");
		printf("|--------- Smart Home Device Management System ---------|\n");
		printf("|  > Welcome > Login > Admin > Delete Device            |\n");
		printf("|-------------------------------------------------------|\n");
		printf("|  1. Delete By Name         2. Delete By Type          |\n");
		printf("|  3. Delete By Id           q. Quit                    |\n");
		printf("|                                                       |\n");
		printf("|                                                       |\n");
		printf("|-------------------------------------------------------|\n");
		printf("| Select your operation                                 |\n");
		printf("|-------------------------------------------------------|\n");
		printf("\n");
		printf("---------------------- Device List ----------------------\n");
		printf("Id\tName\tType\tIp\tPower\n");
		show_dev(devlist, showfun_dev_full);
		printf("---------------------------------------------------------\n");

		int op = getchar();
		empty_buffer();
		if('1' == op)
		{
			printf("Input device name: ");
			scanf("%s", delete_dev.dev_name);
			empty_buffer();
			del_dev(devlist, cmpfun_dev_name, &delete_dev);
			break;
		}
		else if('2' == op)
		{
			printf("Input device type: ");
			scanf("%s", delete_dev.dev_type);
			empty_buffer();
			del_dev(devlist, cmpfun_dev_type, &delete_dev);
			break;
		}
		else if('3' == op)
		{
			printf("Input device id: ");
			scanf("%d", &delete_dev.dev_id);
			empty_buffer();
			del_dev(devlist, cmpfun_dev_id, &delete_dev);
			break;
		}
		else if('q' == op)
		{
			break;
		}
		else
		{
			continue;
		}
	}

	return 0;
}

int update_dev_menu(node_t *devlist)
{
	device_t upd_dev;
	memset(&upd_dev, 0, sizeof(upd_dev));

	while(1)
	{
		system("clear");
		printf("|--------- Smart Home Device Management System ---------|\n");
		printf("|  > Welcome > Login > Admin > Update Device            |\n");
		printf("|-------------------------------------------------------|\n");
		printf("|  1. Update Device Name     2. Update Device Type      |\n");
		printf("|  3. Update Device Ip       q. Quit                    |\n");
		printf("|                                                       |\n");
		printf("|                                                       |\n");
		printf("|-------------------------------------------------------|\n");
		printf("| Select your operation                                 |\n");
		printf("|-------------------------------------------------------|\n");
		printf("\n");
		printf("---------------------- Device List ----------------------\n");
		printf("Id\tName\tType\tIp\tPower\n");
		show_dev(devlist, showfun_dev_full);
		printf("---------------------------------------------------------\n");

		int op = getchar();
		empty_buffer();
		
		if('1' == op)
		{
			printf("Input device id: ");
			scanf("%d", &upd_dev.dev_id);
			empty_buffer();
			printf("Input new device name: ");
			scanf("%s", upd_dev.dev_name);
			empty_buffer();
			update_dev(devlist, upd_dev.dev_id, (update_fun_t)updatefun_dev_name, &upd_dev);
			break;
		}
		else if('2' == op)
		{
			printf("Input device id: ");
			scanf("%d", &upd_dev.dev_id);
			empty_buffer();
			printf("Input new device type: ");
			scanf("%s", upd_dev.dev_type);
			empty_buffer();
			update_dev(devlist, upd_dev.dev_id, (update_fun_t)updatefun_dev_type, &upd_dev);
			break;
		}
		else if('3' == op)
		{
			printf("Input device id: ");
			scanf("%d", &upd_dev.dev_id);
			empty_buffer();
			printf("Input new device ip: ");
			scanf("%s", upd_dev.dev_ip);
			empty_buffer();
			update_dev(devlist, upd_dev.dev_id, (update_fun_t)updatefun_dev_ip, &upd_dev);
			break;
		}
		else if('q' == op)
		{
			break;
		}
		else
		{
			continue;
		}
	}

	return 0;
}

int show_dev_menu(node_t *devlist)
{
	while(1)
	{
		system("clear");
		printf("|--------- Smart Home Device Management System ---------|\n");
		printf("|  > Welcome > Login > Admin > Display Device           |\n");
		printf("|-------------------------------------------------------|\n");
		printf("|  1. Order By Name          2. Order By Type           |\n");
		printf("|  3. Order By Id            q. Quit                    |\n");
		printf("|                                                       |\n");
		printf("|                                                       |\n");
		printf("|-------------------------------------------------------|\n");
		printf("| Select your operation.                                |\n");
		printf("|-------------------------------------------------------|\n");
		printf("\n");
		printf("---------------------- Device List ----------------------\n");
		printf("Id\tName\tType\tIp\tPower\n");
		show_dev(devlist, showfun_dev_full);
		printf("---------------------------------------------------------\n");

		int op = getchar();
		empty_buffer();
		if('1' == op)
		{
			sort_dev(devlist, cmpfun_dev_name);
			break;
		}
		else if('2' == op)
		{
			sort_dev(devlist, cmpfun_dev_type);
			break;
		}
		else if('3' == op)
		{
			sort_dev(devlist, cmpfun_dev_id);
			break;
		}
		else if('q' == op)
		{
			break;
		}
		else
		{
			continue;
		}
	}

	return 0;
}

int find_dev_menu(node_t *devlist)
{
	device_t cmpdev;
	node_t *temp = NULL;
	while(1)
	{
		system("clear");
		printf("|--------- Smart Home Device Management System ---------|\n");
		printf("|  > welcome > Login > Admin > Find Device              |\n");
		printf("|-------------------------------------------------------|\n");
		printf("|  1. Find By Name           2. Find By Type            |\n");
		printf("|  3. Find By Id             q. Quit                    |\n");
		printf("|                                                       |\n");
		printf("|                                                       |\n");
		printf("|-------------------------------------------------------|\n");
		printf("| Select your operation.                                |\n");
		printf("|-------------------------------------------------------|\n");
		printf("\n");
		printf("---------------------- Device List ----------------------\n");
		printf("Id\tName\tType\tIp\tPower\n");
		show_dev(devlist, showfun_dev_full);
		printf("---------------------------------------------------------\n");

		int op = getchar();
		empty_buffer();
		if('1' == op)
		{
			printf("Input target device name: ");
			scanf("%s", cmpdev.dev_name);
			empty_buffer();
			temp = find_dev(devlist, cmpfun_dev_name, &cmpdev);
			if(temp == NULL || is_empty_dlist(temp))
			{
				printf("No devices found!\n");
			}
			else
			{
				printf("------------------- Search Results --------------------\n");
        		printf("Id\tName\tType\tIp\tPower\n");
        		show_dev(temp, showfun_dev_full);
        		printf("-------------------------------------------------------\n");
			}
			getchar();
			break;
		}
		else if('2' == op)
		{
			printf("Input target device type: ");
			scanf("%s", cmpdev.dev_type);
			empty_buffer();
			temp = find_dev(devlist, cmpfun_dev_type, &cmpdev);
			if(temp == NULL || is_empty_dlist(temp))
			{
				printf("No devices found!\n");
			}
			else
			{
				printf("------------------- Search Results --------------------\n");
        		printf("Id\tName\tType\tIp\tPower\n");
        		show_dev(temp, showfun_dev_full);
        		printf("-------------------------------------------------------\n");
			}
			getchar();
			break;
		}
		else if('3' == op)
		{
			printf("Input target device id: ");
			scanf("%d", &cmpdev.dev_id);
			empty_buffer();
			temp = find_dev(devlist, cmpfun_dev_id, &cmpdev);
			if(temp == NULL || is_empty_dlist(temp))
			{
				printf("No devices found!\n");
			}
			else
			{
				printf("------------------- Search Results --------------------\n");
        		printf("Id\tName\tType\tIp\tPower\n");
        		show_dev(temp, showfun_dev_full);
        		printf("-------------------------------------------------------\n");
			}
			getchar();
			break;
		}
		else if('q' == op)
		{
			return -1;
		}
		else
		{
			continue;
		}
	}

	destroy_dlist(&temp);
	return 0;
}
